/*
 * Created on Jan 19, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package littlecyberwar.ui;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import littlecyberwar.model.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 * 
 * There are many different types of messages that goes into the ChatOutput area:
 * 1. Game messages that's FINAL (a command carried out from you or your opponent)
 * 		These messages are always sent to the backing stores. 
 * 2. Chat Messages (from both you or others). These are also sent to the backing store
 * 
 * NOTE that the temp messages (i.e tentative movement), or tool help text 
 * (i.e. please select a unit) are to be sent here. 
 * Temp message should go to the main board near the mouse (eventually)
 * Tool help text should go to the label.  
 * 
 * 
 */
/**
 * @author Milton  Soong
 *
 */
public class ChatPanel extends JPanel {
	private final static String newline = "\n";
	private final static Color _chatMsgColor = Color.BLACK;
	private final static Color _gameMsgColor = Color.RED;
	
	protected JTextField chatInput = new JTextField();
	JTextPane chatOutput = new JTextPane();
	JLabel statusLabel;
	int maxHeight;
	InternalCmdFacade icmd=null;
//	private SimpleAttributeSet chatAttrs;
//	private SimpleAttributeSet msgAttrs;
	
	public ChatPanel( JLabel l ) {
		super(new GridBagLayout());
		// chatPanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.weightx = 0.5;
		c.weighty = 0.5;
		c.gridx=0;
		c.gridy=0;
		c.gridheight =4;
		c.ipady = 40;
		c.fill=GridBagConstraints.BOTH;
		chatOutput.setEditable( false );
		JScrollPane chatOutSP = new JScrollPane( chatOutput );
		chatOutSP.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
		chatOutSP.setHorizontalScrollBarPolicy (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER  );
		this.add( chatOutSP, c );
	
		c.gridx=0;
		c.gridy=4;
		c.ipady = 0;
		c.gridheight =1;
		this.add( chatInput, c );
		Font f = chatInput.getFont();
		chatOutput.setFont( f );
		
		c.gridy=5;
		statusLabel = l;
		this.add( statusLabel, c );
		
		// want to set the maxHeight and Preferred height to a maximum value
		//
		int tfieldHeight = chatInput.getPreferredSize().height;
		maxHeight = tfieldHeight*5;
	
		// now put in the action handler stuff
		chatInput.addActionListener(new ActionListener()  {
		public void actionPerformed(ActionEvent e) {
		 chatInput_actionPerformed(e);
		}
		});
		
	}
	
	public Dimension getPreferredSize() {
		Dimension d = super.getPreferredSize();
		d.height = maxHeight;
		return (d);
	}
	
	public Dimension getMaximumSize () {
		return (getPreferredSize());
	}
	
	public void chatInput_actionPerformed(ActionEvent e) {
	  String text = chatInput.getText();
	  ChatCommand chatCmd = new ChatCommand(text);
	  chatCmd.setPlayerName(Players.getSelfName());
	  icmd.addCmd(chatCmd);
	  chatInput.setText("");
	}

	public void addChatMsg( final String msg, final String playerName ) {
		Players thisPlayer = Players.getPlayers(playerName);
		final SimpleAttributeSet chatAttrs = thisPlayer.getChatAttrs();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				addOutput( msg, chatAttrs, playerName );
			}
		});
	}
	
	public void addGameMsg( final String msg, final String playerName ) {
		Players thisPlayer = Players.getPlayers(playerName);
		final SimpleAttributeSet msgAttrs = thisPlayer.getMsgAttrs();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				addOutput( msg, msgAttrs, playerName );
			}
		});
	}
	
	private synchronized void addOutput( String msg, AttributeSet attrs, String playerName ) {
		
		Document doc = chatOutput.getDocument();
		String totalMsg = playerName + ">" + msg + "\n";
		try {
		  doc.insertString(doc.getLength(), totalMsg, attrs);
		} catch (BadLocationException ex) {
			ex.printStackTrace(); 
		}
		// now make sure we scroll to the end
		Dimension d = chatOutput.getPreferredSize();
		int h = d.height;
		Rectangle rec = new Rectangle(0, h-1, 1, 1);
		chatOutput.scrollRectToVisible(rec);
	}
	
	public void setInternalCmdFacade(InternalCmdFacade facadeIn) {
		icmd=facadeIn; 
	}

	public InternalCmdFacade getInternalCmdFacade() {
		return ( icmd );
	}

}
